<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="excel" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          default-expand-all
          :props="{
            children: 'children',
            label: 'manager'
          }"
        >
          <template #default="{data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>  {{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="excel" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog :title="isEdit?'编辑':'添加'" :visible.sync="showDialog" :close-on-click-modal="false" :close-on-press-escape="false">
        <AddorEdit v-if="showDialog" :id="curId" :is-edit="isEdit" :origin-list="originList" @hSuccess="hSuccess" @close="close" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartment, delDepartmentById } from '@/api/modules/departments'
import { transDataToTreeData } from '@/utils/index'
import AddorEdit from './deptDiglog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      list: [], // 收集企业的部门信息
      showDialog: false, // 用来控制弹窗的显示与隐藏
      curId: '', // 用来保存传递过去的id参数
      isEdit: false, // 用来控制是编辑页还是增加页
      originList: [] // 用来装子组件需要效验的数据
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    close() {
      this.showDialog = false
    },
    // 删除当前部门
    hDel(id) {
      this.$confirm('您确定要删除吗').then(async() => {
        const res = await delDepartmentById(id)
        console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadDepartment()
      }).catch(err => err)
    },
    // 操作成功之后的动作
    hSuccess() {
      this.showDialog = false
      this.loadDepartment()
    },
    // 点击添加打开弹窗并赋值id,方便传给子集
    hAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    // 点击编辑打开弹窗并赋值id
    hEdit(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = true
    },
    // 获取企业的部门列表
    async loadDepartment() {
      try {
        const res = await getDepartment()
        console.log(res)
        res.data.depts.shift()
        this.originList = res.data.depts.map(item => { return { code: item.code, id: item.id, name: item.name, pid: item.pid } })
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
